<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\Responder;

use StudyBuddy\WebPage;
use StudyBuddy\Paginator;
use StudyBuddy\Template\Urhere;
use StudyBuddy\Request;


/**
 * Controller for rendering
 * the "Members" page
 *
 * If request is by Ajax, it returns only the content
 * of the main area, paginated, sorted and with pagination
 * links if necessary
 *
 */
class Listflag extends WebPage
{
	/**
	 * Users to show per page
	 *
	 * @var int
	 */
	protected $perPage = 3;

	protected $pagerPath = '/listflag';


	/**
	 * Will show this page
	 * in 1-column layout, no right side nav
	 *
	 * @var int
	 */
	protected $layoutID = 1;


	/**
	 * Indicates the current tab
	 *
	 * @var string
	 */
	protected $qtab = 'flags';


	/**
	 * Value of the sort $_GET param
	 *
	 * @var string
	 */
	protected $sort = 'new';


	/**
	 * Condition for MongoCursor sort
	 *
	 * Defaults to sort by reputation in
	 * Descending order
	 *
	 * @var array
	 */
	protected $sortOrder = array('h_ts' => -1);


	/**
	 * Mongo Cursor
	 *
	 * @var object of type MongoCursor
	 */
	protected $oCursor;


	/**
	 * Total number of users
	 *
	 * @var int
	 */
	protected $flagsCount;

	/**
	 * Html block with users
	 *
	 * @var string html
	 */
	protected $flagsHtml;
	
	protected $permission = 'delete_answer';


	protected function main(){

		$this->init()
		->getCursor()
		->paginate()
		->renderflagsHtml();

		/**
		 * In case of Ajax request, just return
		 * the content of the flagsHtml
		 * and don't proceed any further
		 */
		if(Request::isAjax()){
			Responder::sendJSON(array('paginated' => $this->flagsHtml));
		}

		$this->setTitle()
		->makeSortTabs()
		//->makeTopTabs()
		->setUsers();
	}


	/**
	 * Set value for title meta and title on page
	 *
	 * @todo translate string
	 *
	 * @return object $this
	 */
	 
	protected function getUser(){
		$uid = $this->oRequest->get('uid', 'i', null);
		if($uid && ($uid !== $this->oRegistry->Viewer->getUid())){
			/**
			 * This is edit tutor for another user
			 * check Viewer permission here
			 */
			$this->checkAccessPermission($permission);
			$this->oUser = \StudyBuddy\User::factory($this->oRegistry)->by_id($uid);
		} else {
			$this->oUser = $this->oRegistry->Viewer;
		}

		return $this;
	}
	protected function setTitle(){
		$title = 'Flags question or answer in past 7 days';
		$this->aPageVars['title'] = $title;
		$this->aPageVars['qheader'] = '<h1>'.$title.'</h1>';

		return $this;
	}


	/**
	 * Initialize some instance variables
	 * based on "sort" request param
	 *
	 * @throws \InvalidArgumentException if sort param is invalid
	 *
	 * @return object $this
	 */
	protected function init(){
		$this->getUser();
		$this->perPage = 20;

		$this->sort = $this->oRegistry->Request->get('sort', 's', 'new');
		if(!in_array($this->sort, array('new', 'old'))){
			throw new \InvalidArgumentException('Invalid value of "sort" param. Valid values are "new", "old". Was: '.$this->sort);
		}

		switch($this->sort){

			case 'new':
				$this->sortOrder = array('_id' => -1);
				$this->pagerPath = '/listflag';
				break;


			case 'old':
				$this->sortOrder = array('_id' => 1);
				$this->pagerPath = '/listflag';
				break;
		}

		return $this;
	}


	/**
	 * Sets top tabs for the page
	 * making the "Members" the current active tab
	 *
	 * @return object $this
	 */
	protected function makeTopTabs(){
            $guest = null;
            if($this->oRegistry->Viewer->isGuest()) {
                $guest['link_ask'] = '#'; // 13
                $guest['link_ask_click'] = ' onclick="oSL.getQuickRegForm()"'; //14
            }
		d('cp');
		$tabs = Urhere::factory($this->oRegistry)->get('tplToptabs', $this->qtab, $guest);
		$this->aPageVars['topTabs'] = $tabs;

		return $this;
	}



	/**
	 * Paginate the results of cursor
	 *
	 * @return object $this
	 */
	protected function paginate(){

		d('paginating');
		$oPaginator = Paginator::factory($this->oRegistry);
		$oPaginator->paginate($this->oCursor, $this->perPage,
		array('path' => $this->pagerPath));

		$this->pagerLinks = $oPaginator->getLinks();

		d('$this->pagerLinks: '.$this->pagerLinks);

		return $this;
	}


	/**
	 * Sets the value of "sort by" tabs
	 *
	 * Will not add any tabs if there are fewer than 4 users on the site
	 * because there are just 4 "sort by" tabs
	 * and there is no need to sort the results
	 * of just 4 items
	 *
	 * @return object $this
	 */
	protected function makeSortTabs(){

		$tabs = '';

		/**
		 * Does not make sense to show
		 * any type of 'sort by' when
		 * Total number of users is
		 * fewer than number of "sort by" tabs
		 */
		if($this->flagsCount > 4){

			$tabs = Urhere::factory($this->oRegistry)->get('tplFlagstypes', $this->sort);
		}

		return $this;

	}


	/**
	 * Runs the find() in the USERS collection
	 * and sets the $this->oCursor instance variable
	 *
	 * @return object $this
	 */
	protected function getCursor(){
		$where = array('i_ts' => array('$gt' => (time() - 604800)));
		$this->oCursor = $this->oRegistry->Mongo->REPORTED_ITEMS ->find($where);

			$this->oCursor->sort($this->sortOrder);
			$this->flagsCount = $this->oCursor->count();

			return $this;
	}


	/**
	 * Renders the content of the members block
	 * and sets the $this->flagsHtml instance variable
	 * but does not yet add them to page
	 * The Ajax based request will just
	 * grab the result of this variable
	 *
	 * @return object $this
	 */
	protected function renderflagsHtml(){
	
		$this->flagsHtml = '<div class="ctl-ct badgeswrap">
							<h3>Reported abuse items</h3>
							<div class="flags">'
							.\tplFlagslist::loop($this->oCursor).
							'</div></div>'.$this->pagerLinks.'';
		
		return $this;
	}


	/**
	 * Adds the content of users block
	 * to page body
	 *
	 * @return object $this
	 */
	protected function setUsers(){
	
		$this->aPageVars['body'] .= '<div id="" class="sortable paginated" StudyBuddy:total="'.$this->flagsCount.'" StudyBuddy:perpage="'.$this->perPage.'">'.$this->flagsHtml.'</div>';

		return $this;
	}

}
